package com.easylinkin.linkappapi.safe.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.safe.dao.InspectionPlanMapper;
import com.easylinkin.linkappapi.safe.entity.InspectionPlan;
import com.easylinkin.linkappapi.safe.service.InspectionTaskService;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Author: kan yuanfeng
 * @Date: 2022/5/25 15:41
 * @Description: 预警定时任务
 */
@Component
public class PlanTimer {

  @Autowired
  private InspectionTaskService inspectionTaskService;

  @Autowired
  private InspectionPlanMapper inspectionPlanMapper;

  private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(
      PlanTimer.class);

  @Scheduled(cron = "0 6 4 * * ?")
  public void dayCheck(){
    //查询已发布的计划
    try {
      LOGGER.info("dayCheck定时任务开始---------------dayCheck------------start--");
      QueryWrapper<InspectionPlan> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("publishing_",2);
      List<InspectionPlan> inspectionPlans = inspectionPlanMapper.selectList(queryWrapper);
      inspectionPlans.forEach(i->{
        inspectionTaskService.insertByPlan(i,true);
      });
      LOGGER.info("dayCheck定时任务完成---------------dayCheck------------end--");
    }catch (Exception e){
      LOGGER.info("dayCheck定时任务异常---------------dayCheck------------error--",e);
    }
  }

  /**
   * 定期计划
   */
  @Scheduled(cron = "0 0 7 ? * 2")
  public void weekCheck(){
    //查询已发布的周计划并且没有到期的
    try {
      LOGGER.info("weekCheck定时任务开始---------------weekCheck------------start--");
      QueryWrapper<InspectionPlan> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("publishing_",2).eq("type_",1)
          .ge("end_time_",new Date())
          .le("start_time_",new Date());
      List<InspectionPlan> inspectionPlans = inspectionPlanMapper.selectList(queryWrapper);
      inspectionPlans.forEach(i ->{
        inspectionTaskService.insertByPlan(i,false);
      });
      LOGGER.info("weekCheck定时任务完成---------------weekCheck------------end--");
    }catch (Exception e){
      LOGGER.error("weekCheck定时任务异常---------------weekCheck------------error--",e);
    }

  }

  /**
   * 月度计划
   */
  @Scheduled(cron = "0 0 7 1 * ?")
  public void monthCheck(){
    //查询已发布的周计划并且没有到期的
    try {
      LOGGER.info("monthCheck定时任务开始---------------monthCheck------------start--");
      QueryWrapper<InspectionPlan> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("publishing_",2).eq("type_",2)
          .ge("end_time_",new Date())
          .le("start_time_",new Date());
      List<InspectionPlan> inspectionPlans = inspectionPlanMapper.selectList(queryWrapper);
      inspectionPlans.forEach(i ->{
        inspectionTaskService.insertByPlan(i,false);
      });
      LOGGER.info("monthCheck定时任务完成---------------monthCheck------------end--");
    }catch (Exception e){
      LOGGER.error("monthCheck定时任务异常---------------monthCheck------------error--");
    }

  }
}
